import { api } from '../boot/axios'
import { Version } from './type'

function url(s: string) {
  if (s==''){
    return ['/api/versions', ''].join('/')
  }
  return ['/api/versions', s.replace('/', ''), ''].join('/')
}

export async function listVersions(stock_code?:string){
  stock_code = stock_code?.toUpperCase()
  const res = await api({
    url: url(''),
    method: 'get',
    params: {stock_code}
  })
  return res.data as Version[]
}

export async function getVersion (id:number){
  const res = await api({
    url: url(''+id),
    method: 'get',
  })
  return res.data as Version
}

export async function patchVersion (id:number,data:Partial<Version>={}){
  const res = await api({
    url: url(''+id),
    method: 'patch',
    data
  })
  return res.data as Version
}

export async function createVersion (data:Partial<Version>={}){
  const res = await api({
    url: url(''),
    method: 'post',
    data
  })
  return res.data as Version
}

export async function deleteVersionMaster(id:number) {
  const res = await api({
    url: url(''+id),
    method: 'delete',
  })
  return res.data as Version
}

export async function softDeleteVersionMaster(id:number) {
  const res = await api({
    url: url('soft_delete_version_master'),
    method: 'delete',
    data:{id}
  })
  return res.data as Version
}